﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Seleccion
''' </summary>
''' <remarks></remarks>
Public Class DALSeleccion

    Private Shared m_Instance As DALSeleccion
    Private Shared m_Semaforo As New Object
    Private m_TDGSeleccion As TDGSeleccion



    Public Function Sel(obj As cSeleccion) As cSeleccion
        Dim dr As SqlDataReader
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGSeleccion.Find(database.Conexion, obj.oBusqueda.Busq_Numero, obj.Sel_Numero)

            If dr.HasRows Then

                dr.Read()

                obj.oBusqueda.Busq_Numero = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.Sel_Numero = Utils.Convert.ToInt16(dr.GetValue(1))
                obj.Sel_Fecha = Utils.Convert.ToDateTime(dr.GetValue(2))
                obj.oTipo_Seleccion.TSel_Codigo = Utils.Convert.ToInt16(dr.GetValue(3))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(4))
                obj.oTipo_Seleccion.TSel_Descrip = Utils.Convert.ToStr(dr.GetValue(5))

                dr.Close()
            Else
                obj = Nothing
            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.SeleccionFalloBuscar(ex)
        End Try
    End Function

    Public Function SelectPorBusqueda(iBusq_Numero As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cSeleccion
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGSeleccion.FindAllPorBusqueda(database.Conexion, iBusq_Numero)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cSeleccion

                    objAux.oBusqueda.Busq_Numero = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.Sel_Numero = Utils.Convert.ToInt16(dr.GetValue(1))
                    objAux.Sel_Fecha = Utils.Convert.ToDateTime(dr.GetValue(2))
                    objAux.oTipo_Seleccion.TSel_Codigo = Utils.Convert.ToInt16(dr.GetValue(3))
                    objAux.DVH = Utils.Convert.ToInt32(dr.GetValue(4))
                    objAux.oTipo_Seleccion.TSel_Descrip = Utils.Convert.ToStr(dr.GetValue(5))
                    objAux.PostulantesSeleccionados = Utils.Convert.ToInt16(dr.GetValue(6))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.SeleccionFalloConsultar(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALSeleccion
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALSeleccion
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGSeleccion = TDGSeleccion.Instance()
    End Sub

End Class
